package com.sq.swing.frame;

import java.awt.Component;
import java.awt.Container;
import java.awt.FocusTraversalPolicy;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JTextField;

public class ViewerFocusTraversalPolicy extends FocusTraversalPolicy {

	List<Component> focusOrderList = new ArrayList<Component>();

	public void addComponent(Component component) {
		focusOrderList.add(component);
	}

	/**
	 * 
	 * @param focusCycleRoot
	 *            Container
	 * @param aComponent
	 *            Component
	 * @return Component
	 */
	public Component getComponentAfter(Container focusCycleRoot,
			Component aComponent) {

		int idx = (focusOrderList.indexOf(aComponent) + 1)
				% focusOrderList.size();

		Component component = focusOrderList.get(idx);

		boolean bContinue = false;

		if (!component.isEnabled()) {
			bContinue = true;
		} else if (component instanceof JTextField) {
			JTextField txtField = (JTextField) component;
			if (!txtField.isEditable() || !txtField.isEnabled()) {
				bContinue = true;
			}
		} else if (component instanceof JButton) {
			JButton button = (JButton) component;
			if (!button.isEnabled()) {
				bContinue = true;
			}
		}

		if (bContinue) {
			return getComponentAfter(focusCycleRoot, component);
		} else {
			return component;
		}
	}
	
	/**
	 * 
	 * @param focusCycleRoot
	 *            Container
	 * @param aComponent
	 *            Component
	 * @return Component
	 */
	public Component getComponentBefore(Container focusCycleRoot,
			Component aComponent) {

		int nIDx = focusOrderList.indexOf(aComponent);

		int nBefore;
		if (nIDx == 0) {
			nBefore = focusOrderList.size() - 1;
		} else {
			nBefore = nIDx - 1;
		}

		Component component = focusOrderList.get(nBefore);

		boolean bContinue = false;

		if (!component.isEnabled()) {
			bContinue = true;
		} else if (component instanceof JTextField) {
			JTextField txtField = (JTextField) component;
			if (!txtField.isEditable()) {
				bContinue = true;
			}
		} else if (component instanceof JButton) {
			JButton button = (JButton) component;
			if (!button.isEnabled()) {
				bContinue = true;
			}
		}

		if (bContinue) {
			return getComponentBefore(focusCycleRoot, component);
		} else {
			return component;
		}

	}

	/**
	 * 
	 * @param focusCycleRoot
	 *            Container
	 * @return Component
	 */
	public Component getDefaultComponent(Container focusCycleRoot) {
		Component component = focusOrderList.get(0);
		return component;
	}

	/**
	 * 
	 * @param focusCycleRoot
	 *            Container
	 * @return Component
	 */
	public Component getLastComponent(Container focusCycleRoot) {
		Component component = focusOrderList.get(focusOrderList.size() - 1);
		return component;
	}

	/**
	 * 
	 * @param focusCycleRoot
	 *            Container
	 * @return Component
	 */
	public Component getFirstComponent(Container focusCycleRoot) {
		Component component = focusOrderList.get(0);
		return component;
	}
}